//
//  XGL_MessageChatImageView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/28.
//

import UIKit
import JXPhotoBrowser
import AVKit

class XGL_MessageChatImageView: UIView {
    
    var coverView: UIImageView!
    lazy var playItem: UIImageView = {
        return UIImageView().then { make in
            make.isHidden = true
            make.image = imageWithName("icon_video_play")
        }
    }()
    lazy var animationView: XGL_CustomHudView = {
        return XGL_CustomHudView().then { make in
            make.backgroundColor = .black.withAlphaComponent(0.15)
        }
    }()
    lazy var failedView: UIView = {
        let view = UIView().then { make in
            make.backgroundColor = UIColor.black.withAlphaComponent(0.15)
        }
        let icon = UIImageView(image: imageWithName("img_xgl_message_send_failed"))
        view.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(24.0)
        }
        return view
    }()
    
    var messageData: XGL_MessageChatData = XGL_MessageChatData(){
        didSet{
            switch self.status {
            case .Success:
                self.playItem.isHidden = self.messageData.messageType != 2
                break
            default:
                self.playItem.isHidden = true
                break
            }
            if self.messageData.messageType == 1 || self.messageData.messageType == 2{
                if let coverData = self.messageData.cover,let image = UIImage(data: coverData){
                    self.coverView.image = image
                }
            }
        }
    }
    
    var status: XGLMessageStatus = .Success{
        didSet{
            switch self.status {
            case .Failed:
                self.failedView.isHidden = false
                self.animationView.isHidden = true
                self.playItem.isHidden = true
                break
            case .Sending:
                self.failedView.isHidden = true
                self.animationView.isHidden = false
                self.playItem.isHidden = true
                break
            case .Success:
                self.failedView.isHidden = true
                self.animationView.isHidden = true
                self.playItem.isHidden = self.messageData.messageType != 2
                break
            }
        }
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_MessageChatImageView{
    @objc private func playItemAction(){
        if self.messageData.messageType == 1{
            guard let imageData = self.messageData.cover,let image = UIImage(data: imageData) else { return }
            let images = [image]
            let browser = JXPhotoBrowser()
            browser.numberOfItems = {
                images.count
            }
            browser.reloadCellAtIndex = { context in
                let browserCell = context.cell as? JXPhotoBrowserImageCell
                browserCell?.imageView.image = images[context.index]
            }
            browser.pageIndex = 0
            browser.show()
        }else if self.messageData.messageType == 2{
            guard let window = UIApplication.shared.currentWindow,let controller = window.visibleViewController() else { return  }
            if let url = self.messageData.videoPath{
                let player = AVPlayer(url: url)
                let playerViewController = AVPlayerViewController()
                playerViewController.player = player
                controller.present(playerViewController, animated: true)
            }
        }
    }
}
extension XGL_MessageChatImageView{
    private func createXglView(){
        self.layerCornerRadius = 12.0
        self.clipsToBounds = true
        self.coverView = UIImageView()
        let button = UIButton().then { make in
            make.addTarget(self, action: #selector(self.playItemAction), for: .touchUpInside)
        }
        self.addSubviews([self.coverView,self.animationView,self.failedView,self.playItem,button])
        self.coverView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.animationView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.failedView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.playItem.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(36.0)
        }
        button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
